// pages/footprint/footprint.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
// var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
// var qqmapsdk;

Page({

  /**
   * 页面的初始数据
   */
  data: {
     addressCol:false,
     freight:'0.00'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   //   qqmapsdk = new QQMapWX({
   //      key: '25FBZ-XOBW3-CHH3V-YBMKG-IVNWT-FYBRE'
   //   });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.getaddress();
     this.setData({
        items: wx.getStorageSync('orderList')
     })
     this.dataProcess();
  },
  /**
  * 获取默认地址
  */
   getaddress:function(){
      let that = this;
      api.POST({
         params:{},
         url: app.globalData.base_url + 'address/info',
         success: function (res) {
            if (util.isEmpty(res.data.code)) {
               if (res.data.data.aid){
                  that.setData({
                     addressCol:true,
                     username: res.data.data.username,
                     aid: res.data.data.aid,
                     mobile: res.data.data.mobile,
                     address: res.data.data.address,
                     province: res.data.data.province.name,
                     city: res.data.data.city.name,
                     town: res.data.data.town.name,
                     street: res.data.data.street.name
                  })
                  that.getFreight();
               }else{
                  that.setData({
                     addressCol: false
                  })
               }
            } else {
               util.showError(res.data.message);
            }
         },
         fail: function (fail) {
            util.showError("网络错误");
         }
      })
   },
   /**
    * 地址解析
    */
   // addressAnalyze:util.debounce(function(){
   //    let that = this;
   //    let address = that.data.province + that.data.city + that.data.town + that.data.street + that.data.address
   //    qqmapsdk.geocoder({
   //       //获取表单传入地址
   //       address: address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
   //       success: function (res) {//成功后的回调
   //          var res = res.result;
   //          var latitude = res.location.lat;
   //          var longitude = res.location.lng;
   //       },
   //       fail: function (error) {
   //          console.error(error);
   //       },
   //       complete: function (res) {
   //          console.log(res);
   //       }
   //    }) 
   // }),


   /**
    * 数据处理
    */
   dataProcess:function(){
      var list = [];
      if (this.data.items.length<=0){
         return false
      }
      for (var i = 0; i < this.data.items.length; i++) {
         var box = {};
         box.sku_code = this.data.items[i].sku_code
         box.num = this.data.items[i].num
         // if (this.data.items[i].inn_id) {
         //    box.inn_id = this.data.items[i].inn_id
         // }
         // if (this.data.items[i].shop_id) {
         //    box.shop_id = this.data.items[i].shop_id
         // }
         // box.price = this.data.items[i].current_price
         list.push(box);
      }
      this.setData({
         productList: list
      })

   },

   /**
    * 获取运费
    */
   getFreight:function(){
      let that =this
      const params = new Object();
      params.aid = this.data.aid;
      params.record = JSON.stringify(this.data.productList);

      api.POST({
         params: params,
         url: app.globalData.base_url + 'address/getFreight',
         success: function (res) {
            if (util.isEmpty(res.data.code)) {
               that.setData({
                  freight:res.data.data.totalCarriage
               })
            } else {
               util.showError(res.data.message);
            }
         },
         fail: function (fail) {
            util.showError("网络错误，请检查网络");
         }
      })

   },

   /**
    * 提交订单
    */
    submit:util.debounce(function(){
       let that = this
       const params = new Object();
       params.aid = this.data.aid;
       params.record = JSON.stringify(this.data.productList);
       params.use_type=2;
       api.POST({
          params: params,
          url: app.globalData.base_url + 'winecabinet/use',
          success: function (res) {
             if (util.isEmpty(res.data.code)) {
               var oid = res.data.data.result.oid;
               if (that.data.freight>0){
                 that.wxPayOrder(oid);
               }else{
                 wx.redirectTo({
                   url: '/index/pages/paySuccessful/paySuccessful?oid=' + oid
                 })
               }
             } else {
                util.showStatus(res.data.message)
             }
          },
          fail: function (fail) {
             util.showError("网络错误，请检查网络");
          }
       })
    }),

   /**
    * 支付接口
    */
   wxPayOrder: function (oid) {
      var params = new Object()
      var that = this;
      params.oid = oid;
      api.POST({
         params: params,
         url: app.globalData.base_url + 'payment/getWechatInfo',
         success: function (res) {
            if (util.isEmpty(res.data.code)) {
               var preData = res.data.data.payinfo;
               wx.requestPayment({
                  timeStamp: preData.timeStamp,
                  nonceStr: preData.nonceStr,
                  package: preData.package,
                  signType: preData.signType,
                  paySign: preData.paySign,
                  success: function (res) {
                     wx.redirectTo({
                        url: '/index/pages/paySuccessful/paySuccessful?oid=' + oid
                     })
                  },
                  fail: function (error) {
                     util.showError("支付失败");
                     wx.removeStorageSync('orderList');
                     setTimeout(function () {
                        wx.redirectTo({
                           url: '/center/pages/orderDetails/orderDetails?oid=' + oid
                        })
                     }, 1000)
                  }
               })
            } else {
               util.showError(res.data.message);
            }
         },
         fail: function (fail) {
            util.showError("网络错误，请检查网络");
         }
      })
   },



   addAddress: util.debounce(function(){
      wx.navigateTo({
         url: '/index/pages/addAddress/addAddress',
      })
   }),
   addressList: util.debounce(function () {
      wx.navigateTo({
         url: '/index/pages/addressList/addressList',
      })
   })
})